package com.teamwings.modules.follow.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teamwings.modules.follow.entity.OatAppFollow;
import com.teamwings.modules.follow.mapper.OatAppFollowMapper;
import com.teamwings.modules.follow.moduleMsg.ModuleType;
import com.teamwings.modules.follow.service.IOatAppFollowService;
import com.teamwings.util.UUIDGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * 表单设计服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-01-06
 */
@Service
@Slf4j
public class OatAppFollowServiceImpl extends ServiceImpl<OatAppFollowMapper, OatAppFollow> implements IOatAppFollowService {

    @Override
    public List<OatAppFollow> findByByInstId(String instId) {
        return this.baseMapper.findByByInstId(instId);
    }

    @Override
    public void save(String instId, String userId) {
        OatAppFollow oatAppFollow;
        List<OatAppFollow> oatAppFollows = this.baseMapper.findByInstIdAndUserId(instId, userId,null);
        if (oatAppFollows.size() > 0) {
            oatAppFollow = oatAppFollows.get(0);
            if (oatAppFollow.getFState() == 0) {
                return;
            }
            oatAppFollow.setFState(0);
        } else {
            oatAppFollow = new OatAppFollow();
            oatAppFollow.setFId(UUIDGenerator.getUUID());
            oatAppFollow.setFInstId(instId);
            oatAppFollow.setFUserId(userId);
            oatAppFollow.setFState(0);
        }
        this.saveOrUpdate(oatAppFollow);
    }

    @Override
    public void updateStateByInstIdAndUserId(String instId, String userId, Integer state) {
        this.baseMapper.updateStateByInstIdAndUserId(instId, userId, state);
    }

    @Override
    public void sendMessage(String instId, ModuleType moduleType) {

    }

    @Override
    public boolean isFollow(String instId, String userId) {
        return this.baseMapper.findByInstIdAndUserId(instId, userId,0).size() > 0 ? true : false;
    }
}
